package com.fept.msgcore.msgserver.netty.channel;

import io.netty.channel.Channel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Srma on 1/12/2017.
 */
public class ChannelManager {

    private static Logger logger = LoggerFactory.getLogger(ChannelManager.class);
    private String channelManagerName = "";
    private static Map<String, ChannelManager> channelManagers = new ConcurrentHashMap();
    private Map<String, Channel> channelMap = new ConcurrentHashMap();

    public static ChannelManager getChannelManager(String name) {
        ChannelManager cm = (ChannelManager)channelManagers.get(name);
        if(cm == null) {
            cm = new ChannelManager(name);
            channelManagers.put(name, cm);
        }

        return cm;
    }

    private ChannelManager(String name) {
        this.setChannelManagerName(name);
    }

    public void addChannel(String channelID, Channel c) {
        logger.debug("add channel");
        this.channelMap.put(channelID, c);
    }

    public void removeChannel(String channelID) {
        logger.debug("remove channel");
        this.channelMap.remove(channelID);
    }

    public Channel getChannel(String channelID) {
        return (Channel)this.channelMap.get(channelID);
    }

    public List<Channel> listCHChannels() {
        ArrayList channels = new ArrayList();
        channels.addAll(this.channelMap.values());
        return channels;
    }


    public String getChannelManagerName() {
        return this.channelManagerName;
    }

    public void setChannelManagerName(String channelManagerName) {
        this.channelManagerName = channelManagerName;
    }
}
